import * as actionTypes from './constans'
const defaultState = {
    movieslist:[],
    actorslist:[]
}
export default (state=defaultState,action) => {
     switch (action.type) {
        case actionTypes.CHANGE_MOVIE_LIST:
            return {
               ...state,
               movieslist:action.data
            }
        case actionTypes.CHECK_MOVIE:
            // 在reducer 重新计算前的状态 ？ 旧状态
            let checkList = state.movieslist; 
            checkList.map(item => {
                if (item.id == action.data) {
                    item.check = !item.check
                    console.log(item.check)
                    // 0  1  - 
                    item.count == '0' ? item.count = '1' : ''
                }
            })
            // 新状态 
            return  Object.assign({}, state, {
                movieslist: [...checkList]
            })
            break;
        case actionTypes.CHNAGE_MOVIES_NUM:
            let changeList = state.movieslist;
            //  + -  指定商品  action type CHNAGE_GOODS_NUM 
            //  data: {id:id, status:'add|minus' }
            changeList.map((item) => {
                if (item.id == action.data.id ) {
                    action.data.status == 'add'? item.count++: item.count--;
                    item.count == '0' ? item.check = false : ''
                    // -1 UI 去做 item.goodsNum> 0 && <button>-</button>
                }
            })
            return Object.assign({}, state, {movieslist: [...changeList]})
            break;
        case actionTypes.CHANGE_ACTORS:
            return {
                ...state,
                actorslist:action.data
            }
        default:
			return state
    }
}